const Article = require("../model/article")
const User = require("../model/user")
const HttpException = require('../exception/http.exception.js');
const Tag = require("../model/tag")

function handleArticle(article, author, count, favorited) {
    //标签处理
    const tags = []
    for (const tag of article.dataValues.tags) {
        tags.push(tag.name)
    }
    article.dataValues.tags = tags
    //作者信息处理
    delete author.dataValues.password
    delete article.dataValues.userEmail
    article.dataValues.author = author

    //处理喜欢
    article.dataValues.favoritedCount = count//喜欢数量
    article.dataValues.favorited = favorited  //是否喜欢

    return article.dataValues
}

//添加喜欢
module.exports.addFavorite = async (req, res, next) => {
    try {

        //文章参数slug
        const { slug } = req.params
        console.log(slug);
        //获取文章:是否存在.(两步)
        let article = await Article.findByPk(slug, { include: Tag })
        if (!article) {
            throw new HttpException(404, '文章不存在', 'article not found')
        }

        //给当前文章添加喜欢的用户
        // console.log(article.__proto__);
        const userEmail = req.user.email

        // const user = await User.findByPk(userEmail)

        //粉丝信息 当前登录用户的email
        await article.addUsers(userEmail)
        // const user3=await article.addUser('cailing@qq.com')  //手动添加了一个粉丝

        //返回数据准备:
        //1作者信息
        const author = await article.getUser()     //获取作者

        const countFavorite = await article.countUsers()    //获取所有关注者
        // console.log(countFavorite);
        // 2喜欢数量/是否喜欢
        article = handleArticle(article, author, countFavorite, true)
        // 3文章

        return res.status(200)
            .json({
                status: 1,
                message: "添加喜欢成功",
                data: article
            })
    } catch (error) {
        next(error)
    }
}

//取消喜欢
module.exports.removeFavorite = async (req, res, next) => {
    try {
        //获取slug
        const { slug } = req.params
        //获取文章:是否存在.(两步)
        let article = await Article.findByPk(slug, { include: Tag })
        if (!article) {
            throw new HttpException(404, '文章不存在', 'article not found')
        }
        //给当前文章添加喜欢的用户（用户：登录账号的人）
        const userEmail = req.user.email
        const user = await User.findByPk(userEmail)

        //粉丝信息 当前登录用户的email
        // console.log(article.__proto__);
        //以下三种都可以取消对文章的喜欢
        const user1 = await article.removeUser(userEmail)
        // const user1 = await article.removeUsers(userEmail)   //取消对文章的喜欢
        // const user1 = await article.removeUser("cailing@qq.com") //这样也可以删除 但要使email  slug  和token对应上
        // console.log(user1);

        //返回数据准备:
        //1作者信息
        const author = await article.getUser()     //获取作者

        const countFavorite = await article.countUsers()    //获取所有关注者
        // console.log(countFavorite);
        // 2喜欢数量/是否喜欢
        article = handleArticle(article, author, countFavorite, false)
        // 3文章
        return res.status(200)
            .json({
                status: 1,
                message: '取消喜欢文章成功',
                data: article
            })
    } catch (error) {
        next(error)
    }
}
